const moment = require("moment");
const utils = require('../../util/utils');
const menuDao = require('../../dao/admin/menuDao');

var MenuService = module.exports;

var genError = function (errCode, message) {
    var error = new Error(message);
    error.errCode = errCode;
    return error;
};

/**
 * 加载顶级菜单
 * @param cb
 */
MenuService.getParentMenu = function(cb) {
    menuDao.getParentMenu().then(data => {

        return utils.invokeCallback(cb, null, data);

    }).catch( err => {

        console.log('Unable to connect to the database:'+err.stack);
        return utils.invokeCallback(cb, {error: 1, msg: '获取父级菜单失败，请联系管理'});
    });
};

/**
 * 获取所有菜单
 * @param cb
 */
MenuService.getAllMenu = function(cb) {
    menuDao.getAllMenu().then(data => {
        return utils.invokeCallback(cb, null, data);
    }).catch( err => {
        console.log('Unable to connect to the database:'+err.stack);
        return utils.invokeCallback(cb, {error: 1, msg: '获取菜单失败，请联系管理'});
    });
};

/**
 * 加载菜单列表
 * @param keywords
 * @param start
 * @param length
 * @param cb
 */
MenuService.getMenus = function (keywords, start, length, cb) {
    menuDao.getMenus(keywords, start, length).then(data => {
        if (!data) {
            return utils.invokeCallback(cb, {error: 1, msg: '内部错误'});
        }

        var result = {count: data.count, rows:[]};
        data.rows.forEach(function (menu) {
            result.rows.push(menu);
        });

        return utils.invokeCallback(cb, null, result);
    }).catch( err => {

        console.log('Unable to connect to the database:'+err.stack);
        return utils.invokeCallback(cb, {error: 1, msg: '内部错误'});
    });
};

/**
 * 创建菜单
 * @param menuInfo
 * @param cb
 */
MenuService.createMenu = function(menuInfo, cb){
    const menu_name = menuInfo.menu_name;
    menuDao.getMenuByName(menu_name).then(menu => {
        if (menu) {
            throw genError(1, '菜单已存在');
        }

        menuDao.createMenu(menuInfo).then(newMenu => {

            utils.invokeCallback(cb, null, newMenu);
        }).catch(function (err) {

            console.log('Unable to connect to the database:'+err.stack);
            utils.invokeCallback(cb, err, null);
        });
    }).catch(err => {

        console.log('Unable to connect to the database:'+err.stack);
        utils.invokeCallback(cb, err);
    });
};

/**
 * 更新菜单
 * @param menuInfo
 * @param cb
 */
MenuService.updateMenu = function(menuInfo, cb){
    const menu_id = menuInfo.menu_id;
    menuDao.getMenuById(menu_id).then(menu => {
        if (!menu) {
            throw genError(1, '菜单不存在');
        }

        menuDao.updateMenu(menuInfo).then(result => {

            utils.invokeCallback(cb, null, result);
        }).catch(function (err) {

            console.log('Unable to connect to the database:'+err.stack);
            utils.invokeCallback(cb, err);
        });
    }).catch(err => {
        console.log('Unable to connect to the database:'+err.stack);
        utils.invokeCallback(cb, err);
    });
};

/**
 * 删除菜单
 * @param ids
 * @param cb
 */
MenuService.destroyMenus = function (ids, cb) {
    menuDao.getParentMenus(ids).then(results => {
        if (results && results.length != 0){
            return utils.invokeCallback(cb, {error: 1, msg: '删除项有子菜单，需要先删除子菜单'});
        }

        menuDao.destroyMenus(ids).then(result => {
            return utils.invokeCallback(cb, null, result);
        }).catch(err => {
            console.log('Unable to connect to the database:'+err.stack);
            return utils.invokeCallback(cb, {error: 1, msg: '删除失败，请联系管理员'});
        });
    }).catch(err => {
        console.log('Unable to connect to the database:'+err.stack);
        return utils.invokeCallback(cb, {error: 1, msg: '删除失败，请联系管理员'});
    });
};